package com.platinadesign.axa.manager
{
	import com.greensock.TweenLite;
	import com.platinadesign.axa.BaseManager;
	import com.platinadesign.axa.event.PageTurningEvent;
	import com.platinadesign.axa.view.Page;
	import com.platinadesign.axa.view.Preloader;
	import com.platinadesign.axa.view.SystemViewContainer;
	import com.platinadesign.axa.view.TurningPages;
	import com.platinadesign.axa.vo.Resource;
	import com.platinadesign.axa.vo.ResourceLoadingVO;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class LayoutManager extends BaseManager
	{
		private var _app : main;

		private var _preloader : Preloader;

		private var _view : SystemViewContainer;

		private var _endPageIndex : uint;
		
		private var _oldScaleValue:Number;
		
		private var _oldX:Number;
		
		private var _oldY:Number;

		public function LayoutManager( app : main )
		{
			super();
			_app = app;
			initListener();
		}

		public function get view():SystemViewContainer
		{
			return _view;
		}

		public function initView( resource : Resource ) : void
		{
			_view = initViewContainer( resource );
			validateLayout();

			_app.addChild( _view );
		}

		public function setProgress( loadingVO : ResourceLoadingVO ) : void
		{
			//trace( loadingVO.currentIndex + " , " + loadingVO.totalAmount + " , " + loadingVO.resourceURL );
			_preloader.setProgress( loadingVO.currentIndex, loadingVO.totalAmount, loadingVO.resourceURL )
		}

		public function initPreloader() : void
		{
			_app.addChild( initPreloaderBar());
			
		}

		protected function validateLayout( event : Event = null ) : void
		{
			if( _view )
			{
				const scale : Number = Math.min(1,(( _app.stage.stageHeight - 100 ) / _view.pageContainer.height));
				_view.pageContainer.width *= scale;
				_view.pageContainer.height *= scale;
				_view.pageContainer.x = _oldX = ( _app.stage.stageWidth ) * 0.5
				_view.pageContainer.y = _oldY = 50;
				
				_oldScaleValue = scale;
				_view.background.width = _app.stage.stageWidth;
				_view.background.height = _app.stage.stageHeight;
			}
		}

		private function initPreloaderBar() : Preloader
		{
			_preloader = new Preloader( Progress_Bar );
			_preloader.x = (_app.stage.stageWidth!=0)?( _app.stage.stageWidth - _preloader.width) * 0.5:100;
			_preloader.y = (_app.stage.stageWidth!=0)?( _app.stage.stageHeight - _preloader.height ) * 0.5:200;
			_preloader.body['perloaderx'].htmlText =""; //_app.stage.stageWidth+","+_preloader.width+","+_preloader.x;//
			_preloader.body['perloadery'].htmlText ="";// _app.stage.stageHeight+","+_preloader.height+","+_preloader.y;
			return _preloader;
		}

		private function initListener() : void
		{
			this.addViewListener( Event.RESIZE, validateLayout );
			this.addViewListener( TurningPages.ALL_PAGES_TURNED, allPagesTurnedHandler );
			this.addSystemListener( PageTurningEvent.PAGE_TURNING, startPageTurning );
			this.addSystemListener( "TOGGLE_ZOOM", zoomHandler);
		}
		
		private function zoomHandler(e:Event):void
		{
			toggleZoom(_view.pageContainer);
		}
		private function allPagesTurnedHandler( event : Event ) : void
		{
			_view.stopTurning( _endPageIndex );
		}

		private function startPageTurning( event : PageTurningEvent ) : void
		{
			_view.hidePages();
			const endPage : Page = event.overPages[ event.overPages.length - 1 ];
			_endPageIndex = endPage.index;
			_view.startTurning( event.direction, event.overPages );
		}
		private function initViewContainer( resource : Resource ) : SystemViewContainer
		{
			const viewContainer : SystemViewContainer = new SystemViewContainer();
			viewContainer.init( resource );
			return viewContainer;
		}
		private function toggleZoom(container:Sprite):void
		{
			if(container.scaleX != 1){
				const new_X:Number = container.mouseX - container.width*0.5;
				const new_Y:Number = container.mouseY - container.height*0.5;
				TweenLite.to(container,1,{x:-new_X, y:-new_Y, scaleX:1, scaleY:1, onComplete: addDrag });
			}
			else{
				TweenLite.to(container,1,{x:_oldX, y:_oldY, scaleX:_oldScaleValue, scaleY:_oldScaleValue, onComplete: removeDrag });
			}
		}
		
		private function addDrag ():void{
			if(!_view.pageContainer.hasEventListener(MouseEvent.MOUSE_DOWN)){
				_view.pageContainer.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
				_view.pageContainer.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
				addViewListener(MouseEvent.MOUSE_OUT,mouseUpHandler);
			}
		}
		private function mouseUpHandler(event:MouseEvent = null):void
		{
			_view.pageContainer.stopDrag();
		}
		
		private function mouseDownHandler(event:MouseEvent):void
		{
			_view.pageContainer.startDrag();
		}
		private function removeDrag():void{
			if(_view.pageContainer.hasEventListener(MouseEvent.MOUSE_DOWN)){
				_view.pageContainer.removeEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
				_view.pageContainer.removeEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
				removeViewListener(MouseEvent.MOUSE_OUT,mouseUpHandler);
			}
		}
	}
}